//
//  SearchResultRowCellView.swift
//  Hermes
//
//  Created by FH on 2021/1/26.
//  Copyright © 2021 Agora. All rights reserved.
//

import Foundation

class SearchResultRowCellView : NSTableCellView, FLTableRowCellable {
    let yoga = FLContext()
    let lblType = FLLabel()
    let lblAPIName = FLLabel()
    let lblParentName = FLLabel()
    var bgView: NSView! = nil
    
    static func cellHeight(by dataItem: Any) -> CGFloat {
        return 30
    }
    
    func updateData(_ data: Any) {
        if let item = data as? AstNode {
            var type = ""
            switch item.type {
            case .enumerate:
                type = "E"
                self.lblType.textColor = .brown
            case .interface:
                type = "C"
                self.lblType.textColor = .darkGray
            case .protocol:
                type = "I"
                self.lblType.textColor = .darkGray
            case .method:
                type = "M"
                self.lblType.textColor = .blue
            case .property:
                type = "P"
                self.lblType.textColor = .yellow
            default:
                type = ""
            }
            self.lblType.flText = "[\(type)]"
            self.lblAPIName.flText = item.name
            self.lblParentName.flText = item.parentName ?? ""
            self.lblParentName.isHidden = item.parentName == nil
        }
    }
    
    func render(_ table: NSTableView) {
        if self.bgView == nil {
            self.bgView = NSView()
            self.bgView.bindAsRoot(to: yoga) { it in
                it.direction = .row
                it.paddingLeft = 4
                it.alignItems = .center
                it.width = table.bounds.size.width.toFLValue()
                it.height = 30
            }
            
            self.lblType.bind(to: yoga, self.bgView) { it in
                it.width = 32
            }
            self.lblType.font = NSFont.systemFont(ofSize: 16)
            self.lblType.alignment = .center

            self.lblAPIName.bind(to: yoga, self.bgView) { it in
                it.marginLeft = 4
                it.marginRight = 2
                it.flexGrow = 1
            }
            self.lblAPIName.font = NSFont.systemFont(ofSize: 13)
            
            yoga.apply()
            self.addSubview(self.bgView)
        }
    }
    
    // TODO: show full api name when width > 420
    func startMouseOver() {
        self.bgView.flBackgroundColor = .lightGray
    }
    
    func stopMouseOver() {
        self.bgView.flBackgroundColor = .white
    }
}
